package com.chineseall.app.controller;

import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.websocket.Session;

import jmind.core.spring.AbstractController;
import jmind.core.util.CollectionsUtil;
import jmind.core.util.DataUtil;
import jmind.core.util.RequestUtil;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.chineseall.app.AppConst;
import com.chineseall.app.ConnectionManager;
import com.chineseall.app.MTypes;
import com.chineseall.app.pojo.mongo.MMessage;
import com.chineseall.app.ws.SendMessage;
import com.google.common.collect.Lists;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.util.JSON;

@RequestMapping("/op")
@Controller
public class OpWSController extends AbstractController {

    @RequestMapping(value = "/send", method = RequestMethod.GET)
    public String sendNotify() {
        return "/op/notify";
    }

    @RequestMapping(value = "/send", method = RequestMethod.POST)
    @ResponseBody
    public String send(final HttpServletRequest request, @RequestParam(defaultValue = AppConst.TITLE) String title,
            @RequestParam(defaultValue = "") String body, @RequestParam(defaultValue = "") String url,
            @RequestParam(defaultValue = MTypes.NOTIFY_301 + "") int mtype,
            @RequestParam(defaultValue = "0") int directType) {
        MMessage message = new MMessage();
        message.mtype(mtype);
        message.url(url);
        message.title(title);
        message.body(body);
        String summary = request.getParameter("summary");
        if (!DataUtil.isEmpty(summary))
            message.summary(summary);
        String param = request.getParameter("param");
        if (!DataUtil.isEmpty(param)) {
            message.param((DBObject) JSON.parse(summary));
        }
        List<String> ll = Lists.newArrayList();
        if (directType == 1) { // 全部用户
            Set<Session> sessions = ConnectionManager.INSTANCE.getOpenSessions();
            for (Session s : sessions) {
                ll.add(s.getId() + s.getUserProperties() + SendMessage.send(s, message));
            }
        } else if (directType == 2) { // 某一版本的用户
            String u = RequestUtil.getParam(request, "u", "");
            Set<Session> sessions = ConnectionManager.INSTANCE.getOpenSessions();
            for (Session s : sessions) {
                BasicDBObject info = (BasicDBObject) s.getUserProperties().get("info");
                String ver = (String) info.get("ver");
                if (u.equals(ver)) {
                    ll.add(s.getId() + s.getUserProperties() + SendMessage.send(s, message));
                }
            }
        } else if (directType == 3) { // 特定uid 用户， 用逗号分隔
            String u = RequestUtil.getParam(request, "u", "");
            List<Integer> list = CollectionsUtil.asIntListGtZero(u, ",");
            for (Integer uid : list) {
                ll.add(uid + "=" + SendMessage.send(uid, message));
            }

        }
        return ll.toString();
    }

}
